var marker;
var map;
var area;

function getAreaPosition()
{
	var area_name = $("#area_name").val();
	if($.trim(area_name) == '')
	{
		alert("请输入商圈名称");
		return false;
	}
	else
		area_name = ' ' + area_name;
	
	var city_name = $("#city_id option:selected").html();
	var parent_name = $("#parent_id option:selected").html();
	
	if(parent_name == '顶级商圈')
		parent_name = '';
	else
		parent_name = ' ' + parent_name;
	

	var address = city_name + parent_name + area_name;
	var longitude = $("#longitude").val();
	var latitude = $("#latitude").val();
	
	if(Math.abs(longitude) > 0 && Math.abs(latitude) > 0)
	{
		var latLng = new GLatLng(latitude,longitude);
		showMapWindow(latLng,"");
	}
	else if(address != "")
	{
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(address,function(latLng){
			if (latLng) 
			{
				showMapWindow(latLng,address);
			}
			else
			{
				alert("查询不到【" + address + "】的定位信息!");
			}
		});
	}
	else
	{
		alert("请输入要查询的的定位地址!");
	}
}

function showMapWindow(latLng,address)
{
	var selfObj=$(".mapwindow");
	
	if(selfObj.length==0)
	{
		var html='<div class="mapwindow"><dl><dt><span>查询定位信息</span><a href="javascript:;" title="关闭">关闭</a></dt><dd><div id="googleMap"></div></dd></dl></div>';
		$("body").append(html);
		selfObj=$(".mapwindow");
		
		$(window).scroll(function(){
			if(selfObj.css("display") != "none")
			{
				$.windowCenter(selfObj);
				$(bgDiv).css({width:$(document).width(), height:$(document).height()});
			}
		});
	}
	
	var bgDiv=document.createElement("DIV");
	
	$("body").append(bgDiv);
	$(bgDiv).css({position:"absolute",width:$(document).width(), height:$(document).height(),top:"0",left:"0",opacity:0.8,background:"#fff",display:"none","z-index":100});
	$.windowCenter(selfObj);
	selfObj.show();
	$(bgDiv).show();
	$(bgDiv).bgiframe();
	
	map = new GMap2($("#googleMap").get(0));
	map.enableScrollWheelZoom();
	map.addControl(new GLargeMapControl3D());
	map.addControl(new GMenuMapTypeControl());
	
	map.setCenter(latLng,13);
	marker = new GMarker(latLng,{title:address,draggable:true,bouncy:true,dragCrossMove:true});
	map.addOverlay(marker);
	
	$(bgDiv).click(function(){
		$(bgDiv).remove();
		selfObj.css({display:"none"});
		closeMapWindow();
	});
	
	$("dt a",selfObj).click(function(){
		$(bgDiv).remove();
		selfObj.css({display:"none"});
		closeMapWindow();
	});
}

function closeMapWindow()
{
	$("#longitude").val(marker.getLatLng().lng());
	$("#latitude").val(marker.getLatLng().lat());
	GUnload();
}